package com.pym.controller;


import com.pym.common.ResultMsg;
import com.pym.entity.PymWpgj;
import com.pym.enums.ResultEnum;
import com.pym.service.WpgjService;
import com.pym.service.WpglService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = {"物品轨迹操作接口"})
@RestController
@RequestMapping("/wpgj")
public class WpgjController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WpgjService wpgjService;

    @Autowired
    private WpglService wpglService;

    @ApiOperation(value = "根据物品信息ID查询物品轨迹接口")
    @RequestMapping(value = "/queryWpgjByWpid/", method = RequestMethod.GET, produces = "application/json")
    public ResultMsg queryWpgjByWpid(@RequestParam(value = "wpId") String wpId) {
        List<PymWpgj> wpgjList = wpgjService.queryWpgjByWpid(wpId);
        return new ResultMsg(wpgjList);
    }


    /**
     * @Author Jerry
     * @Description //TODO 物品信息出入库操作接口
     * @Date 10:58 2021/8/20
     * @Param pymWpgj 物品轨迹实体
     * @Param wpId 物品信息主键
     **/
    @ApiOperation(value = "物品信息出入库操作接口")
    @RequestMapping(value = "/addWpgjByWpidAndStatus", method = RequestMethod.POST, produces = "application/json")
    @Transactional
    public ResultMsg addWpgjByWpidAndStatus(@RequestBody PymWpgj pymWpgj,
                                            @RequestParam(value = "wpId") String wpId) {
        // 根据ID和执行操作状态查询数据库中物品信息是否存在该数据
        String wpzt = this.wpglService.queryWpztById(wpId);
        logger.info("查询物品状态为：{}",wpzt);
        int result = 0;

        if(StringUtils.hasLength(pymWpgj.getType())){
            // 判断物品状态不为空时进行下面操作
            if(StringUtils.hasLength(wpzt)){
                // 判断物品的状态是否一致，如果一样为执行重复的操作，则返回操作失败提示
                if(pymWpgj.getType().equals(wpzt)){
                    if(wpzt.equals("2")){
                        return new ResultMsg(ResultEnum.WPXX_RUKU_EXIST_ERROR);
                    }else if(wpzt.equals("3")){
                        return new ResultMsg(ResultEnum.WPXX_CHUKU_EXIST_ERROR);
                    }
                }
                // 当前物品状态为（1：已登记，3：已出库）时做入库操作，（2：已入库）时做出库操作
                if(wpzt.equals("1")  || wpzt.equals("2") || wpzt.equals("3")){
                    // 添加物品轨迹操作
                    result = this.wpgjService.addWpgjBywzj(pymWpgj);
                    if(result > 0){
                        // 添加物品轨迹成功后修改物品状态
                        result = this.wpglService.updateWpxxWpztById(wpId, pymWpgj.getType(), pymWpgj.getAddtime().toString());
                        return result <= 0 ?
                                new ResultMsg(ResultEnum.WPXX_WPZT_UPDATE_ERROR) :
                                new ResultMsg(ResultEnum.SUCCESS);
                    }
                    // 添加轨迹失败，返回提示信息
                    return new ResultMsg(ResultEnum.WPGJ_ADD_ERROR);
                } else{
                    // 物品状态不存在，返回提示信息
                    return new ResultMsg(ResultEnum.WPXX_WPZT_NOTEXIST_ERROR);
                }
            }
        }
        // 执行操作状态为空，返回给前端提示信息
        return new ResultMsg(ResultEnum.WPXX_WPZT_NOTEXIST_ERROR);
    }
}
